﻿using System;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public class FontTagAttribute : Attribute
    {
        
    }

    [FontTag]
    public class DefineFontTag : Tag, IHasCharacterId
    {
        public ushort FontID
        {
            get;
            protected set;
        }

        public uint[] OffsetTable
        {
            get;
            protected set;
        }

        public Shape[] GylphShapeTable
        {
            get;
            protected set;
        }

        public ushort NumGylphs
        {
            get;
            protected set;
        }

        public DefineFontTag(int len, int offset)
            : this(TagCodes.DefineFont, len, offset)
        {

        }

        protected DefineFontTag(TagCodes code, int len, int offset)
            : base(code, len, offset)
        {

        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            FontID = reader.ReadUInt16();

            ushort FirstOffset = reader.ReadUInt16();
            
            NumGylphs = (ushort) (FirstOffset / 2);

            OffsetTable = new uint[NumGylphs];
            GylphShapeTable = new Shape[NumGylphs];

            OffsetTable[0] = FirstOffset;

            for (int i = 1; i < NumGylphs; i++)
            {
                OffsetTable[i] = reader.ReadUInt16();                
            }

            for (int i = 0; i < NumGylphs; i++)
            {
                GylphShapeTable[i] = new Shape(ShapeType.Shape1);
                GylphShapeTable[i].SwfDeserialize(reader);
            }
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #region IHasCharacterID Members

        public ushort Id
        {
            get { return FontID; }
        }

        #endregion
    }
}
